﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.Threading;
using System.Globalization;

namespace DalatCooking.Core.Web.Controls
{
    public class BasePage:System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
            /*
            string language = "vi-VN";//DalatGreen.Core.Entities.Settings.Setting("site.language");

            Thread.CurrentThread.CurrentCulture =
                CultureInfo.CreateSpecificCulture(language);
            Thread.CurrentThread.CurrentUICulture = new
                CultureInfo(language);
             * */
            base.InitializeCulture();
        }
        #region Utitlities
       
        protected virtual void AddGlobalStyles(string cssFolder)
        {
            // add styles in the ~/Styles folder to the page header
            // var s = Path.Combine(HttpContext.Current.Server.MapPath("~/"), "css"); 
         
            var s = HttpContext.Current.Server.MapPath(cssFolder);
            var fileEntries = Directory.GetFiles(s);
            foreach (var fileName in
                fileEntries.Where(fileName => fileName.EndsWith(".css", StringComparison.OrdinalIgnoreCase)))
            {
                this.AddStylesheetInclude(
                    string.Format("{0}/{1}", cssFolder, ExtractFileNameFromPath(fileName)), false);
            }
        }
        protected virtual void AddGlobalJs(string jsFolder)
        {
            Uri uriSiteRoot = new Uri(Request.Url.GetLeftPart(UriPartial.Authority));
            var s = HttpContext.Current.Server.MapPath(jsFolder);
            var fileEntries = Directory.GetFiles(s);
            foreach (var fileName in
                fileEntries.Where(fileName => fileName.EndsWith(".js", StringComparison.OrdinalIgnoreCase)))
            {
                this.AddJsInclude(
                    string.Format("{0}{1}/{2}", "", jsFolder, ExtractFileNameFromPath(fileName)));
            }
        }
        public virtual void AddJsInclude(string url)
        {
            HtmlGenericControl js = new HtmlGenericControl("script");
            js.Attributes.Add("type", "text/javascript");
            js.Attributes.Add("src", url);
            this.Page.Header.Controls.Add(js);  
           //ClientScript.RegisterClientScriptInclude("script", url);
        }
        public virtual void AddStylesheetInclude(string url, bool insertAtFront)
        {
            var link = new HtmlLink();
            link.Attributes["type"] = "text/css";
            link.Attributes["href"] = url;
            link.Attributes["rel"] = "stylesheet";

            if (insertAtFront)
            {
                this.Page.Header.Controls.AddAt(0, link);
            }
            else
            {
                this.Page.Header.Controls.Add(link);
            }
        }
        public virtual void AddStylesheetInclude(string url)
        {
            this.AddStylesheetInclude(url, false);
        }
        public static string ExtractFileNameFromPath(string path)
        {
            return Path.GetFileName(path);
        }
        #endregion
       
    }
}
